package storm;

import kafka.javaapi.consumer.ConsumerConnector;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values;

import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * @author chentong
 * @date 2021年12月10日 20：55
 */
public class LogFileSpout extends BaseRichSpout {

    private SpoutOutputCollector collector = null;
    private ConsumerConnector consumer;

    @Override
    public void open(Map map, TopologyContext topologyContext, SpoutOutputCollector spoutOutputCollector) {
        this.collector = spoutOutputCollector;
    }

    @Override
    public void nextTuple() {
        Properties prop=new Properties();
        prop.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.52.140:9092,192.168.52.141:9092,192.168.52.142:9092,192.168.52.143:9092");
        prop.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        prop.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class);
        prop.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG,"30000");
        prop.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false");
        prop.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,"1000");
        //latest收最新的数据 none会报错 earliest最早的数据
        prop.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"latest");
        prop.put(ConsumerConfig.GROUP_ID_CONFIG,"G2");
        //创建消费者
        KafkaConsumer<byte[], byte[]> consumer = new KafkaConsumer<>(prop);
        consumer.subscribe(Collections.singleton("order"));//订阅
        while (true){
            ConsumerRecords<byte[], byte[]> poll = consumer.poll(100);
            Iterator<ConsumerRecord<byte[], byte[]>> iterator = poll.iterator();
            while (iterator.hasNext()) {
                ConsumerRecord<byte[], byte[]> next = iterator.next();
                String orderinfo = String.valueOf(next.value());
//                System.out.println("-------------------1--------------------");
//                System.out.println(orderinfo);
//                System.out.println("----------------------------------------");
                this.collector.emit(new Values(orderinfo));
            }
        }
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
        outputFieldsDeclarer.declare(new Fields("orderinfo"));
    }
}